<?php

namespace app\api\controller\v1\store\login;

use app\api\controller\Api;
use app\api\controller\v1\untils\Sms;
use app\api\services\v1\user\UserService;

class Login extends Api
{

    private  $services;
    public function initialize()
    {
         parent::initialize();
         $this->services =  $this->app->make(UserService::class);
    }

    /**x
     * 密码登陆
     * @return void
     */
    public function index(){

        $rule = [
            'phone'    => 'require|mobile',
            'password'    => 'require',
        ];
        $message  =   [
            'phone.require' => '手机号不为空',
            'phone.mobile' => '手机号错误',
            'password.require' => '密码不为空',
        ];

        $this->validate($this->post, $rule,$message);

        $this->post['usertype'] = 1;
        $service =  $this->services->login($this->post,$this->request->ip());
        if(!empty($service)){
            $this->success('登陆成功',$service);
        }else{
            $this->error('登陆失败,手机号或密码错误');
        }
    }


    /**
     * 验证码登陆
     * @return void
     */
    public function code(){
        $rule = [
            'phone'    => 'require|mobile',
            'code'    => 'require',
        ];
        $message  =   [
            'phone.require' => '手机号不为空',
            'phone.mobile' => '手机号错误',
            'code.require' => '密码不为空',
        ];

        $this->validate($this->post, $rule,$message);
        $this->post['usertype'] = 1;
//        验证手机验证码
        if (Sms::checkVerifyCode($this->post['code'], $this->post['phone'])) {
            $service =  $this->services->login($this->post,$this->request->ip());
            if(!empty($service)){
                $this->success('登陆成功',$service);
            }else{
                $this->error('登陆失败,手机号错误');
            }
        } else {
            $this->error('短信验证失败！');
        }

    }

}